package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.CostDetailMapper;
import com.koron.css2.baseConfig.queryBean.CostDetailQueryBean;
import com.koron.css2.baseConfig.vo.CostDetailVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DictionaryMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.DictionaryVO;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 费用信息-列表初始化
 * @author 李攀
 */
public class CostDetailList implements ServerInterface  {
	
	private static Logger logger = LogManager.getLogger(CostDetailList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			CostDetailQueryBean bean = JsonUtils.objectToPojo(req.getData(), CostDetailQueryBean.class);
			CostDetailMapper mapper = factory.getMapper(CostDetailMapper.class);	
			DictionaryMapper dmapper = factory.getMapper(DictionaryMapper.class);
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<CostDetailVO> list = mapper.selectCostDetailList(bean);		
			List<DictionaryVO> ladderlist = dmapper.findDictionaryParamName("UWJ");	//阶梯类型，数据字典
			//数据列表数据中的数据字典为中文名称
			for(CostDetailVO vo:list) {
				for(DictionaryVO dvo:ladderlist) {
					if((dvo.getValue()+"").equals(vo.getLadderType())) {
						vo.setLadderType(dvo.getName());
						break;
					}
				}
			}
			info.setData(new PageInfo<>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}